//
//  XTMyCaseViewController.m
//  BeautifulHome
//
//  Created by dlios on 15-7-8.
//  Copyright (c) 2015年 ACE. All rights reserved.
//

#import "XTMyCaseViewController.h"
#import "XTCaseDetailsViewController.h"
#import "XTCase.h"
#import "XTMyLoginViewController.h"
@interface XTMyCaseViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)UITableView *tableview;
@end

@implementation XTMyCaseViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的案例";
    //返回
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"999b"];
    [self.view addSubview:imageView];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(my)];
    [self get];
    [self creatTableView];
    
}
- (void)my
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
//解档
- (void)get
{
    self.arr = [NSMutableArray array];
    //归档解档收藏
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //创建文件管理器(单例)
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //添加文件夹
    //拼接文件夹地址
    NSString *filePath = [path stringByAppendingPathComponent:@"收藏"];
    //创建文件夹
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *strPath = [filePath stringByAppendingPathComponent:@"myCase.pp"];
    self.arr = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
}
//创建TableView
- (void)creatTableView
{
    
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableview];
    [_tableview release];
    
    //开启tableView编辑状态
//    [_tableview setEditing:YES animated:YES];
 
}


#pragma mark - tableView编辑方法
#warning 1.开启编辑状态
//- (void)setEditing:(BOOL)editing animated:(BOOL)animated
//{
//    //调用父类的编辑方法
//    [super setEditing:editing animated:animated];
//    
//    //2.利用系统编辑按钮控制tableView编辑状态
//    [_tableview setEditing:editing animated:animated];
//}
#warning 2.是否允许编辑 canEdit
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#warning 3.设置cell的编辑样式 editingStyle
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    //控制indexPath编辑样式
//    //默认编辑样式为delete
//    return UITableViewCellEditingStyleDelete;
//}
#warning 4.判断编辑状态进行删除或者添加  commitEditingStyle
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //归档解档收藏
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //创建文件管理器(单例)
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //添加文件夹
    //拼接文件夹地址
    NSString *filePath = [path stringByAppendingPathComponent:@"收藏"];
    //创建文件夹
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *strPath = [filePath stringByAppendingPathComponent:@"myCase.pp"];
    //通过editingStyle判断状态,执行删除/添加操作
    //删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSMutableArray *marr = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithFile:strPath]];
        [marr removeObjectAtIndex:indexPath.row];
        
        
        //1.删除cell之前 先从数组中对应位置 删除数据
        [_arr removeObjectAtIndex:indexPath.row];
        //2.删除的信息需要被存放在一个数组中
        NSArray *arr = @[indexPath];
        //3.让视图和数据匹配
        //参数1: 删除信息的数组
        //参数2: 删除操作的动画效果
        [_tableview deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationLeft];
        [NSKeyedArchiver archiveRootObject:marr toFile:strPath];
    }
    
}
//删除样式 title(确认删除)
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _arr.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if ( cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
    }
    cell.backgroundColor = [UIColor clearColor];
    XTCase *mCase = [[XTCase alloc]init];
    mCase = _arr[indexPath.row];
    cell.textLabel.text = mCase.title;
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //给header设置一个自定义视图
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor cyanColor];
    return [view autorelease];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    XTCaseDetailsViewController *caseD = [[[XTCaseDetailsViewController alloc]init]autorelease];
    XTCase *xCase = _arr[indexPath.row];
    caseD.xtCaseXiang = xCase;
        
    caseD.desc = xCase.mYdescription;
    caseD.user1 = xCase.user;
    [self.navigationController pushViewController:caseD animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
